<template>
  <div>
    <van-popup
      v-model="visible"
      position="bottom"
      :style="{ height: '100%' }"
      class="bottom-popup"
      @closed="close"
    >
      <div class="form-wrapper">
        <div class="form-item">
          <div class="form-label">
            <div class="title">交易时间</div>
            <div class="tips">时间跨度不能超过1年</div>
          </div>
          <div class="form-content">
            <div class="input-btn" @click="setDate('time_start')">
              {{formData.time_start}}
              <van-icon name="arrow-down" />
            </div>
            <div style="font-size: 14px; color: #999;">至</div>
            <div class="input-btn" @click="setDate('time_end')">
              {{formData.time_end}}
              <van-icon name="arrow-down" />
            </div>
          </div>
        </div>
        <div class="form-item">
          <div class="form-label">
            <div class="title">时间顺序</div>
          </div>
          <div class="form-content">
            <van-button size="normal" type="warning" round>由近及远</van-button>
            <van-button size="normal" round>由远及近</van-button>
          </div>
        </div>
        <div class="form-item">
          <div class="form-label">
            <div class="title">交易类型</div>
          </div>
          <div class="form-content">
            <van-button size="normal" type="warning" round>全部</van-button>
            <van-button size="normal" round>收入</van-button>
            <van-button size="normal" round>支出</van-button>
          </div>
        </div>
        <div class="form-item">
          <div class="form-label">
            <div class="title">金额</div>
          </div>
          <div class="form-content">
            <input v-model="price1" class="input-btn" placeholder="最小金额" />
            <div style="font-size: 14px; color: #999;">至</div>
            <input v-model="price2" class="input-btn" placeholder="最大金额" />
          </div>
        </div>
        <div class="form-item">
          <div class="form-label">
            <div class="title">交易摘要</div>
          </div>
          <div class="form-content">
            <van-button size="normal" type="warning" round>全部</van-button>
            <van-button size="normal" round>工资</van-button>
            <van-button size="normal" round>理财</van-button>
            <van-button size="normal" round>基金</van-button>
            <van-button size="normal" round>外汇</van-button>
          </div>
        </div>
        <div class="form-item">
          <div class="form-label">
            <div class="title">交易对方</div>
          </div>
          <div class="form-content">
            <input v-model="price1" class="input-btn" placeholder="对方名称" style="text-align: left;" />
          </div>
          <div class="form-content">
            <input v-model="price1" class="input-btn" placeholder="对方账号" style="text-align: left;" />
          </div>
        </div>
      </div>
      <div class="footer-btn">
        <div class="cancel" @click="cancel">重置</div>
        <div class="confirm" @click="confirm">确定</div>
      </div>
    </van-popup>
    <van-calendar v-model="calendarVisible" @confirm="onConfirm" :min-date="minDate" :max-date="maxDate" />
  </div>
</template>

<script>
import moment from 'moment'
export default {
  model: {
    prop: 'value',
    event: 'input'
  },
  props: ['value', 'form'],
  data() {
    return {
      visible: false,
      formData: {},
      radio: '',
      price1: '',
      price2: '',
      activeField: '',
      calendarVisible: false,
      minDate: new Date(2010, 0, 1),
      maxDate: new Date(2030, 0, 31),
    }
  },
  mounted() {
    this.visible = this.value;
    this.formData = this.form;
  },
  methods: {
    close() {
      this.visible = false;
      setTimeout(()=>{
        this.$emit('input', false);
      },300)
    },
    cancel() {
      this.formData = {};
      this.$emit('change', this.formData)
      this.close();
    },
    confirm() {
      this.$emit('change', this.formData)
      this.close();
    },
    setDate(field) {
      this.activeField = field;
      this.calendarVisible = true;
    },
    onConfirm(date) {
      this.calendarVisible = false;
      this.$set(this.formData, this.activeField, moment(date).format('YYYY-MM-DD'))
    },
  }
}
</script>

<style lang="less" scoped>
  .form-wrapper {
    padding-bottom: 50px;
  }
  .form-label {
    margin: 20px 15px;
    overflow: hidden;
    .title{
      font-size: 16px;
      color: #333;
      font-weight: bold;
      float: left;
    }
    .tips{
      float: right;
      font-size: 12px;
      color: #999;
      margin-top: 2px;
    }
  }
  .form-content {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin: 10px 0;
    flex-wrap: wrap;
    & > *{
      margin: 0 10px;
    }
    .van-button {
      height: 40px;
      line-height: 40px;
      width: 100px;
      margin-bottom: 10px;
    }
  }

  .input-btn{
    width: 100%;
    height: 40px;
    border: 1px solid #eee;
    border-radius: 40px;
    color: #444;
    font-size: 16px;
    text-align: center;
    line-height: 40px;
    flex: 1;
    padding: 0 15px;
    .van-icon {
      margin-left: 15px;
    }
  }

  .footer-btn {
    width: 100%;
    height: 50px;
    position: fixed;
    left: 0;
    bottom: 0;
    overflow: hidden;
    .cancel,.confirm {
      width: 50%;
      height: inherit;
      float: left;
      line-height: 50px;
      text-align: center;
    }
    .cancel {
      background: #ffffff;
      color: orange;
    }
    .confirm {
      color: #ffffff;
      background: orange;
    }
  }
</style>